package com.zds.scm.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zds.scm.common.CommonServlet;
import com.zds.scm.domain.ClassInfo;
import com.zds.scm.domain.DeptInfo;
import com.zds.scm.service.ClassInfoService;
import com.zds.scm.service.DeptInfoService;
import com.zds.scm.service.impl.ClassInfoServiceImpl;
import com.zds.scm.service.impl.DeptInfoServiceImpl;
import static com.zds.scm.common.util.StringUtils.*;
public class ClassInfoServlet extends CommonServlet {

	private static final long serialVersionUID = 4475040933223556033L;
	private ClassInfoService classInfoService=new ClassInfoServiceImpl();
	private DeptInfoService deptInfoService=new DeptInfoServiceImpl();
	private ClassInfo classInfo=new ClassInfo();
	public void setClassInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	   logger.debug("收集表单信息");
	   classInfo.setClassCode(null2Empty(request.getParameter("classCode")));
	   classInfo.setClassName(null2Empty(request.getParameter("className")));
	   DeptInfo deptInfo=new DeptInfo();
	   deptInfo.setDeptCode(null2Empty(request.getParameter("deptCode")));
	   classInfo.setDeptInfo(deptInfo);
	}
	public void insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("执行insert方法");
		try {
			setClassInfo(request, response);
			ClassInfo d1=classInfoService.findById(classInfo.getClassCode());
			if(d1==null){
			  logger.debug("不存在新增");
			  if(classInfoService.insert(classInfo)){
				request.setAttribute("message", "新增系别成功");
				request.setAttribute("statusCode", 200);
				request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
			  }else {
				request.setAttribute("message", "新增系别失败,不存在系别:ClassCode="+classInfo.getClassCode());
				request.setAttribute("statusCode", 300);
				request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
			  }
			}else {
				logger.debug("存在修改");
				if(classInfoService.update(classInfo)){
					request.setAttribute("message", "修改系别成功");
					request.setAttribute("statusCode", 200);
					request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
				}else {
					request.setAttribute("message", "修改系别失败,不存在系别:ClassCode="+classInfo.getClassCode());
					request.setAttribute("statusCode", 300);
					request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
				}
			}
		} catch (Exception e) {
			request.setAttribute("message",e.getMessage());
			request.setAttribute("statusCode", 300);
			request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
		}
	}
	public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("执行delete方法");
		try {
			setClassInfo(request, response);
			if(classInfoService.delete(classInfo.getClassCode())){
				request.setAttribute("message", "删除班级成功");
				request.setAttribute("statusCode", 200);
				request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
			}else {
				request.setAttribute("message", "删除班级失败,不存在班级:ClassCode="+classInfo.getClassCode());
				request.setAttribute("statusCode", 300);
				request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
			}
		} catch (Exception e) {
			request.setAttribute("message",e.getMessage());
			request.setAttribute("statusCode", 300);
			request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
		}
	}
	public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("执行update方法");
		try {
			setClassInfo(request, response);
			if(classInfoService.update(classInfo)){
				request.setAttribute("message", "修改班级成功");
				request.setAttribute("statusCode", 200);
				request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
			}else {
				request.setAttribute("message", "修改班级失败,不存在班级:ClassCode="+classInfo.getClassCode());
				request.setAttribute("statusCode", 300);
				request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
			}
		} catch (Exception e) {
			request.setAttribute("message",e.getMessage());
			request.setAttribute("statusCode", 300);
			request.getRequestDispatcher("/pages/common/ajaxDone.jsp").forward(request, response);
		}
	}
	public void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("执行update方法");
		setClassInfo(request, response);
		request.setAttribute("classInfo", classInfoService.findById(classInfo.getClassCode()));
		request.setAttribute("deptInfos",deptInfoService.findAll());
		request.getRequestDispatcher("/pages/common/classinfoedit.jsp").forward(request, response);
	}
	public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("执行findAll方法");
		setClassInfo(request, response);
		request.setAttribute("classInfos", classInfoService.findAll(getCurrentPage(),getPageSize()));
		request.setAttribute("totalCount", classInfoService.getTotalRows());
		request.setAttribute("deptInfos",deptInfoService.findAll());
		request.getRequestDispatcher("/pages/common/classinfo.jsp").forward(request, response);
	}
	public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("执行findAll方法");
		request.setAttribute("deptInfos",deptInfoService.findAll());
		request.getRequestDispatcher("/pages/common/classinfoedit.jsp").forward(request, response);
	}
	public void findByNameAndCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("执行findByNameAndCode方法");
		setClassInfo(request, response);
		request.setAttribute("deptInfos",deptInfoService.findAll());
		//传给前台计算总页数
		request.setAttribute("totalCount", classInfoService.getTotalRowsByNameAndCode(classInfo.getClassCode(),classInfo.getClassName(),classInfo.getDeptInfo().getDeptCode()));
		//传给前台显示班级信息列表之用
		request.setAttribute("classInfos", classInfoService.findByNameAndCode(classInfo.getClassCode(),classInfo.getClassName(),classInfo.getDeptInfo().getDeptCode(),getCurrentPage(),getPageSize()));
		request.getRequestDispatcher("/pages/common/classinfo.jsp").forward(request, response);
	}
	
}
